package com.example.studyPlatform.controller.api;

import com.example.studyPlatform.controller.api.protocol.API_STATE;
import com.example.studyPlatform.mapper.PostMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import static com.example.studyPlatform.config.BaseConstant.DEFAULT_PAGE_NO;
import static com.example.studyPlatform.config.BaseConstant.DEFAULT_PAGE_SIZE;
import static com.example.studyPlatform.config.RoutingConstant.API_PATH;
import static com.example.studyPlatform.controller.api.kit.JsonUtils.toJson;
import static com.example.studyPlatform.controller.api.kit.JsonUtils.toJsonAttachRemark;

@AllArgsConstructor
@Controller
public class ArchiveApi {
    PostMapper postMapper;

    @ResponseBody
    @GetMapping(API_PATH + "/search")
    ObjectNode search(String keyword, @RequestParam(required = false) Integer pageNo,
                      @RequestParam(required = false) Integer pageSize) {
        if (StringUtils.isBlank(keyword)) {
            return toJsonAttachRemark(API_STATE.CLIENT_ERR, "关键词不能为空");
        }
        // 防止 SQL 注入
        keyword = keyword.replace("\\", "\\\\")
                .replace("%", "\\%")
                .replace("_", "\\_");
        if (pageNo == null) pageNo = DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = DEFAULT_PAGE_SIZE;
        return toJson(API_STATE.SUCCESS, postMapper.search("%" + keyword + "%", pageNo * pageSize, pageSize));
    }

    // 按时间归档
    @ResponseBody
    @GetMapping(API_PATH + "/listByDate")
    ObjectNode sortByDate(@RequestParam(required = false) Integer pageNo,
                          @RequestParam(required = false) Integer pageSize) {
        if (pageNo == null) pageNo = DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = DEFAULT_PAGE_SIZE;
        return toJson(API_STATE.SUCCESS, postMapper.sortByDate(pageNo * pageSize, pageSize));
    }

    //按访问量
    @ResponseBody
    @GetMapping(API_PATH + "/sortByVisits")
    ObjectNode sortByVisit(@RequestParam(required = false) Integer pageNo,
                           @RequestParam(required = false) Integer pageSize) {
        if (pageNo == null) pageNo = DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = DEFAULT_PAGE_SIZE;
        return toJson(API_STATE.SUCCESS, postMapper.sortByVisit(pageNo * pageSize, pageSize));
    }

    //按类别
    @ResponseBody
    @GetMapping(API_PATH + "/listByType")
    ObjectNode getPostsByType(int type, @RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize) {
        if (pageNo == null) pageNo = DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = DEFAULT_PAGE_SIZE;
        return toJson(API_STATE.SUCCESS, postMapper.selectByType(type, pageSize * pageNo, pageSize));
    }


    //根据用户
    @ResponseBody
    @GetMapping(API_PATH + "/listByUser")
    ObjectNode getPostsByUser(int author, @RequestParam(required = false) Integer pageNo,
                              @RequestParam(required = false) Integer pageSize) {
        if (pageNo == null) pageNo = DEFAULT_PAGE_NO;
        if (pageSize == null) pageSize = DEFAULT_PAGE_SIZE;
        return toJson(API_STATE.SUCCESS, postMapper.selectByUser(author, pageNo * pageSize, pageSize));
    }

}
